﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace System
{
  [DebuggerStepThrough]
  public static class WindowsExtensions
  {
    public static void TryInvoke(this Control control, Action action)
    {
      if (control != null && control.InvokeRequired)
      {
        control.Invoke(action);
      }
      else
      {
        action();
      }
    }
    public static T TryInvoke<T>(this Control control, Func<T> func)
    {
      if (control != null && control.InvokeRequired)
      {
        return (T)(control.Invoke(func));
      }
      else
      {
        return func();
      }
    }

    private static readonly Dictionary<TabPage, object[]> _hiddenPages = new Dictionary<TabPage, object[]>();

    public static void SetVisible(this TabPage tabPage, bool visible)
    {
      if (visible)
        tabPage.ShowTabPage();
      else
        tabPage.HideTabPage();
    }
    public static bool IsVisible(this TabPage tabPage)
    {
      var tabControl = tabPage.Parent as TabControl;
      return tabControl != null && tabControl.TabPages.Contains(tabPage);
    }
    public static void ShowTabPage(this TabPage tabPage)
    {
      if (!_hiddenPages.ContainsKey(tabPage))
        return;

      var opt = _hiddenPages[tabPage];
      var tabControl = (TabControl)opt[0];
      var index = (int)opt[1];

      _hiddenPages.Remove(tabPage);
      tabControl.TabPages.Insert(index, tabPage);
    }
    public static void HideTabPage(this TabPage tabPage)
    {
      var tabControl = (TabControl)tabPage.Parent;

      _hiddenPages.Add(tabPage, new object[] { tabControl, tabPage.TabIndex });
      tabControl.TabPages.Remove(tabPage);
    }

    public static int IntValue(this TextBox obj, int defaultVal)
    {
      if (Information.IsNumeric(obj.Text)) return Convert.ToInt32(obj.Text);
      return defaultVal;
    }
    public static int IntValue(this TextBox obj)
    {
      if (Information.IsNumeric(obj.Text)) return Convert.ToInt32(obj.Text);
      return default(int);
    }
    public static double DoubleValue(this TextBox obj, double defaultVal)
    {
      if (Information.IsNumeric(obj.Text)) return Convert.ToDouble(obj.Text);
      return defaultVal;
    }
    public static double DoubleValue(this TextBox obj)
    {
      if (Information.IsNumeric(obj.Text)) return Convert.ToDouble(obj.Text);
      return default(double);
    }


    public static void Export(this DataGridView DGrid, string file_name)
    {
      try
      {
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        ExcelApp.Columns.ColumnWidth = 30;

        for (int i = 0; i < DGrid.Rows.Count; i++)
        {
          DataGridViewRow row = DGrid.Rows[i];
          for (int j = 0; j < row.Cells.Count; j++)
          {
            ExcelApp.Cells[i + 1, j + 1] = row.Cells[j].Value.ToString();
          }
        }
        ExcelApp.ActiveWorkbook.SaveCopyAs(file_name);
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();

      }
      catch (Exception ex)
      {
        ex.Log();
      }
    }

  }
}